﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;

namespace DirectoryWatcher
{
    public class Class
    {
        #region Attributes

        public struct Path
        {
            public string Directory;
            public string ReposPath;
            public string CodePath;
        }

        public static Path StructPath=new Path();
        
        protected static string logpath = ConfigurationManager.AppSettings["LOGPATH"].ToString();

        #endregion

        public Class()
        {
        }

        public Class(Path path)
        {
            StructPath = path;
        }

        public void Start()
        {
            WatcherThread();
            FileStream fs = new FileStream(logpath, FileMode.Append, FileAccess.Write, FileShare.Read);
            StreamWriter txt = new StreamWriter(fs);
            txt.WriteLine(StructPath.Directory);
            txt.Close();
        }

        protected void WatcherThread()
        {
            FileSystemWatcher Watcher = new FileSystemWatcher();
            Watcher.Path = StructPath.Directory;
            Watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size;

            Watcher.Changed += new FileSystemEventHandler(OnChanged);
            Watcher.Created += new FileSystemEventHandler(OnChanged);
            Watcher.Deleted += new FileSystemEventHandler(OnChanged);
            Watcher.Renamed += new RenamedEventHandler(OnChanged);

            Watcher.EnableRaisingEvents = true;
        }

        protected void OnChanged(object source, FileSystemEventArgs e)
        {
            FileStream fs = new FileStream(logpath, FileMode.Append, FileAccess.Write, FileShare.Read);
            StreamWriter txt = new StreamWriter(fs);
            txt.WriteLine(StructPath.Directory+e.ChangeType+e.FullPath);
            txt.Close();
        }
    }
}
